package com.shou.controller.phone;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shou.BusinessException;
import com.shou.CommonVar;
import com.shou.controller.MyController;
import com.shou.entity.Collection;
import com.shou.entity.ServeInfo;
import com.shou.entity.User;
import com.shou.service.impl.ServeInfoService;
import com.shou.service.impl.UserService;

@Controller("phoneUserController")
@RequestMapping("/phone/user")
public class UserController extends MyController{

	@Autowired
	UserService userService;
	
	@Autowired
	ServeInfoService serveInfoService;
	
	/**
	 * 收藏服务
	 * @param collection
	 * @return
	 */
	@RequestMapping("/collectServe")
	public ResponseEntity<String> collectionServe(HttpServletRequest request,Collection collection) {
		Boolean status = true;
		String msg = "收藏成功";
		if(collection.getServeId() == null) {
			status = false;
			msg = "请传入服务id";
		}
		else {			
			try{		
				User user =	(User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
				if(user != null) {					
					collection.setUserId(user.getId());
					userService.collectServe(collection);
				}
				else {
					status = false;
					msg = "please log in correctly";
				}
			}
			catch(BusinessException be){
				status = false;
				msg = be.getErrorMessage();
			} 
			catch(Exception e){
				status = false;
				msg = "收藏失败";
				log.error("用户"+collection.getUserId()+"收藏"+collection.getServeId()+"失败", e);
				e.printStackTrace();
			}
		}
		return renderMsg(status, msg);
	}
	
	/**
	 * 取消收藏
	 * @param userId
	 * @param serveId
	 * @return
	 */
	@RequestMapping("/cancelCollection")
	public ResponseEntity<String> cancelCollcetion(HttpServletRequest request,Long serveId){
		Boolean status = true;
		String msg = "取消成功";
		if(serveId == null) {
			status = false;
			msg = "请传入服务id";
		}
		else {		
			User user =	(User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
			try{	
				if(user!=null) {					
					userService.cancelCollection(user.getId(), serveId);
				}
				else {
					status = false;
					msg = "please log in correctly";
				}
			}
			catch(Exception e){
				status = false;
				msg = "取消收藏失败";
				log.error("用户"+user.getId()+"取消收藏服务"+serveId+"失败",e);
				e.printStackTrace();
			}
		}
		return renderMsg(status, msg);
	}
	
	/**
	 * 跳转到我的收藏页面
	 * @return
	 */
	@RequestMapping("/collection")
	public String collectionPage() {
		return "/phone/user/collection";
	}
	
	/**
	 * 获得收藏列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getMyCollectionList")
	public ResponseEntity<String> getMyCollection(HttpServletRequest request){
		User user =	(User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
		Boolean status = true;
		String msg = "请求成功";
		List<ServeInfo> list = null;
		try{			
			list = userService.getMyCollectServe(user.getId());
		}
		catch(Exception e){
			status = false;
			msg = "请求失败";
			log.error("用户"+user.getId()+"获得收藏列表出错");
			e.printStackTrace();
		}
		return renderData(status, msg, list);
	}
	
	/**
	 * 跳转到我的收藏页面
	 * @return
	 */
	@RequestMapping("/mine")
	public String MyPushPage(HttpServletRequest request,Model model) {
		User user = (User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
		model.addAttribute("user",user);
		return "/phone/user/mine";
	}
	
	/**
	 * 获取我的发布列表信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getMyPushList")
	public ResponseEntity<String> getMyPushList(HttpServletRequest request) {
		User user =	(User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
		Boolean status = true;
		String msg = "请求成功";
		List<ServeInfo> list = null;
		try{			
			if(user!=null) {				
				list = userService.getMyPushInfo(user.getId());
			}
		}
		catch(Exception e){
			status = false;
			msg = "请求失败";
			log.error("用户"+user.getId()+"获得发布列失败",e);
			e.printStackTrace();
		}
		//return renderJsonpData(status, msg, list, request);
		return renderData(status, msg, list);
	}
	
	/**
	 * 删除我的发布信息
	 * @param userId
	 * @param serveId
	 * @return
	 */
	@RequestMapping("/deleteServe")
	public ResponseEntity<String> finishServe(HttpServletRequest request,Long serveId){
		Boolean status = true;
		String msg = "取消成功";
		try{			
			serveInfoService.finishServe(serveId);
		}
		catch(Exception e){
			status = false;
			msg = "取消收藏失败";
			log.error("完成称任务"+serveId+"失败",e);
			e.printStackTrace();
		}
		return renderMsg(status, msg);
		//return renderJsonpMsg(status, msg, request);
	}
}
